package com.jiaai.web.live;

import com.alibaba.dubbo.config.annotation.Reference;

import com.jiaai.auth.model.AccessUser;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.guard.IGuardApi;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 守护
 */
@RequestMapping("/api/v1/app/guard")
@RestController
public class GuardController {
    @Reference
    private IGuardApi guardApi;


    /**
     * 查询指定用户的守护
     * @param mId
     * @return
     */
    @RequestMapping("/list")
    public  Object findByMId(String mId){
        return  RestResultGenerator.genSuccessResult(guardApi.findRankingByMId(mId));
    }

    /**
     * 开通守换
     * @param targetMId
     * @return
     */
    @RequestMapping("/purchase")
    @PostMapping
    @TokenIntercept(checkVisitor = true)
    public  Object purchaseGuard(Integer guardId, String targetMId){
        try {
            AccessUser accessUser= RequestUtils.getUser();
            guardApi.purchase(accessUser.getUserId(),guardId,targetMId);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            return RestResultGenerator.genErrorResult(e.getMessage());
        }
    }

    /**
     * 查询用户开通历史
     * @return
     */
    @RequestMapping("/purchase/history")
    @PostMapping
    @TokenIntercept(checkVisitor = true)
    public  Object purchaseGuardHistory(){
        AccessUser accessUser= RequestUtils.getUser();
        return RestResultGenerator.genSuccessResult(guardApi.findPurchaseHistory(accessUser.getUserId()));
    }


    @GetMapping("/guard/ranking")
    public Object loadGuardRanking(String mId){
        return RestResultGenerator.genSuccessResult(guardApi.findRankingByMId(mId));
    }


}
